{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "'''\n",
    "【课程1.2  Numpy基础数据结构】 课程作业\n",
    "\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 'a' 'hello' [1, 2, 3] {'one': 100, 'two': 200}] \n",
      "a1的shape为： (6,) \n",
      "-------\n",
      "[['0' '1' '2' '3' '4' '5']\n",
      " ['a' 'b' 'c' 'd' 'e' 'f']\n",
      " ['True' 'False' 'True' 'False' 'True' 'True']] \n",
      "a2的shape为： (3, 6) \n",
      "-------\n"
     ]
    }
   ],
   "source": [
    "# 作业1：分别按照要求，生成一个一维数组、二维数组，并且查看其shape\n",
    "\n",
    "a1 = np.array([1,2,'a','hello',[1,2,3],{'one':100,'two':200}])\n",
    "a2 = np.array([list(range(6)), \n",
    "               list('abcdef'), \n",
    "               [True, False, True, False, True, True]])\n",
    "print(a1,'\\na1的shape为：',a1.shape, '\\n-------')\n",
    "print(a2,'\\na2的shape为：',a2.shape, '\\n-------')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 5  6  7  8  9 10 11 12 13 14]\n"
     ]
    }
   ],
   "source": [
    "# 作业2：生成一个一维数组，起始值为5，终点值为15，样本数为10个\n",
    "\n",
    "print(np.arange(5,15))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.  0.  0.  0.]\n",
      " [ 0.  0.  0.  0.]\n",
      " [ 0.  0.  0.  0.]\n",
      " [ 0.  0.  0.  0.]] \n",
      "-------\n",
      "[[ 1.  1.  1.]\n",
      " [ 1.  1.  1.]] \n",
      "-------\n",
      "[[1 0 0]\n",
      " [0 1 0]\n",
      " [0 0 1]] \n",
      "-------\n"
     ]
    }
   ],
   "source": [
    "# 作业3：按照要求创建以下数组\n",
    "\n",
    "print(np.zeros((4,4)), '\\n-------')\n",
    "print(np.ones((2,3)), '\\n-------')\n",
    "print(np.eye(3, dtype='int'), '\\n-------')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "'''\n",
    "【课程1.3  Numpy通用函数】 课程作业\n",
    "\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4]\n",
      " [ 5  6  7  8  9]\n",
      " [10 11 12 13 14]\n",
      " [15 16 17 18 19]] \n",
      "-------\n",
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19  0  1  2  3]\n",
      " [ 4  5  6  7  8  9]] \n",
      "-------\n"
     ]
    }
   ],
   "source": [
    "# 作业1：创建一个20个元素的数组，分别改变成两个形状：(4,5),(5,6) （提示：超出范围用resize）\n",
    "\n",
    "ar = np.arange(20)\n",
    "print(ar.reshape(4,5), '\\n-------')\n",
    "print(np.resize(ar,(5,6)), '\\n-------')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[['0' '1' '2' '3']\n",
      " ['4' '5' '6' '7']\n",
      " ['8' '9' '10' '11']\n",
      " ['12' '13' '14' '15']]\n"
     ]
    }
   ],
   "source": [
    "# 作业2：创建一个(4,4)的数组，把其元素类型改为字符型\n",
    "\n",
    "ar = np.arange(16).reshape(4,4)\n",
    "print(ar.astype(np.str))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "创建数组为：\n",
      " [[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]\n",
      " [12 13 14 15]] \n",
      "-------\n",
      "计算后的数组为：\n",
      " [[100 110 120 130]\n",
      " [140 150 160 170]\n",
      " [180 190 200 210]\n",
      " [220 230 240 250]] \n",
      "-------\n",
      "result的均值为：\n",
      " 175.0 \n",
      "-------\n",
      "result求和为：\n",
      " 2800 \n",
      "-------\n"
     ]
    }
   ],
   "source": [
    "# 作业3：根据要求创建数组，运用数组的运算方法得到结果：result = ar * 10 +100，并求出result的均值及求和\n",
    "\n",
    "ar = np.arange(16).reshape(4,4)\n",
    "print('创建数组为：\\n', ar, '\\n-------')\n",
    "result = ar * 10 +100\n",
    "print('计算后的数组为：\\n', result, '\\n-------')\n",
    "print('result的均值为：\\n', result.mean(), '\\n-------')\n",
    "print('result求和为：\\n', result.sum(), '\\n-------')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "'''\n",
    "【课程1.4  Numpy索引及切片】 课程作业\n",
    "\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4]\n",
      " [ 5  6  7  8  9]\n",
      " [10 11 12 13 14]\n",
      " [15 16 17 18 19]\n",
      " [20 21 22 23 24]] \n",
      "-------\n",
      "ar[4]是：\n",
      " [20 21 22 23 24] \n",
      "-------\n",
      "ar[:2,3:]是：\n",
      " [[3 4]\n",
      " [8 9]] \n",
      "-------\n",
      "ar[3][2]是：\n",
      " 17 \n",
      "-------\n"
     ]
    }
   ],
   "source": [
    "# 作业1：按照要求创建数组，通过索引，其ar[4]、ar[:2,3:]、ar[3][2]分别是多少\n",
    "\n",
    "ar = np.arange(25).reshape(5,5)\n",
    "print(ar, '\\n-------')\n",
    "print('ar[4]是：\\n',ar[4], '\\n-------')\n",
    "print('ar[:2,3:]是：\\n',ar[:2,3:], '\\n-------')\n",
    "print('ar[3][2]是：\\n',ar[3][2], '\\n-------')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0 1 2 3 4]\n",
      " [5 6 7 8 9]] \n",
      "-------\n",
      "大于5的值为： [6 7 8 9]\n"
     ]
    }
   ],
   "source": [
    "# 作业2：按照要求创建数组，筛选出元素值大于5的值并生成新的数组\n",
    "\n",
    "ar = np.arange(10).reshape(2,5)\n",
    "print(ar, '\\n-------')\n",
    "print('大于5的值为：',ar[[ar>5]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "'''\n",
    "【课程1.5  Numpy随机数】 课程作业\n",
    "\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[  0.   1.   2.   3.   4.]\n",
      " [  5.   6.   7.   8.   9.]\n",
      " [ 10.  11.  12.  13.  14.]\n",
      " [ 15.  16.  17.  18.  19.]\n",
      " [ 20.  21.  22.  23.  24.]] \n",
      "-------\n",
      "结果为：\n",
      " [[  0.13205406   0.66160089   2.           3.           4.        ]\n",
      " [  0.76724827   0.15377325   7.           8.           9.        ]\n",
      " [ 10.          11.          12.          13.          14.        ]\n",
      " [ 15.          16.          17.          18.          19.        ]\n",
      " [ 20.          21.          22.          23.          24.        ]] \n",
      "-------\n"
     ]
    }
   ],
   "source": [
    "# 作业1：请按照要求创建数组ar，再将ar[:2,:2]的值改为[0,1)的随机数\n",
    "\n",
    "ar = np.arange(25, dtype = np.float32).reshape(5,5)\n",
    "print(ar, '\\n-------')\n",
    "ar[:2,:2] = np.random.rand(2,2)\n",
    "print('结果为：\\n',ar, '\\n-------')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[-0.24163331  0.05021192  1.68056767 -0.99578621 -0.36021147]\n",
      " [ 0.65763086  1.04544517  0.10924955 -0.46544668 -0.88356707]] \n",
      "-------\n",
      "[[-0.86267133 -0.51950637 -0.57341243  0.16530597 -0.30763428]\n",
      " [-0.60050865 -0.24989034 -0.00126382 -0.2593554  -0.17913029]] \n",
      "-------\n",
      "结果为：\n",
      " [[ -2.41633315e-01   5.02119247e-02   1.68056767e+00  -9.95786207e-01\n",
      "   -3.60211469e-01]\n",
      " [  6.57630859e-01   1.04544517e+00   1.09249550e-01  -4.65446682e-01\n",
      "   -8.83567066e-01]\n",
      " [ -8.62671327e-01  -5.19506370e-01  -5.73412432e-01   1.65305972e-01\n",
      "   -3.07634277e-01]\n",
      " [ -6.00508647e-01  -2.49890342e-01  -1.26382293e-03  -2.59355401e-01\n",
      "   -1.79130288e-01]]\n"
     ]
    }
   ],
   "source": [
    "# 作业2： 创建2个包含10个元素的正太分布一维数组\n",
    "\n",
    "ar1 = np.random.normal(size = (2,5))\n",
    "ar2 = np.random.normal(size = (2,5))\n",
    "print(ar1, '\\n-------')\n",
    "print(ar2, '\\n-------')\n",
    "print('结果为：\\n',np.vstack((ar1,ar2)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "'''\n",
    "【课程1.6  Numpy数据的输入输出】 课程作业\n",
    "\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[65 33 83 14  2 61 69 26 84 11]\n",
      " [42 52 25 77 60 16  7 23  3 19]\n",
      " [ 6 72  3 58 74 27 65 92 61 84]\n",
      " [16 88 11 42 86 66 58 92 58  7]\n",
      " [ 3 72 74 70 36 50 34 70 61 81]\n",
      " [89 87 71 21 46 92 94 43 63 97]\n",
      " [56 81 54 65 74 81 94 92 45 29]\n",
      " [ 9 63 31 36 68 32 67 65 94 52]\n",
      " [21 31 71  2 39 91 86 15 37 53]\n",
      " [ 1 67 61  3 89 16 71 81 48 29]] \n",
      "--------\n",
      "finished!\n"
     ]
    }
   ],
   "source": [
    "# 作业1：创建一个10*10的整数随机数组，取值范围为0-100，并存为txt文件，用逗号分开\n",
    "\n",
    "import os\n",
    "os.chdir('C:/Users/Hjx/Desktop/')\n",
    "\n",
    "ar = np.random.randint(100,size=(10,10))\n",
    "print(ar,'\\n--------')\n",
    "np.savetxt('array.txt',ar, delimiter=',',fmt = '%i')\n",
    "print('finished!')"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
